package com.anysoft.guide;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.home.HomeActivity;
import com.rd.PageIndicatorView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wwq on 2016/12/5.
 */

public class GuideGifActivity extends BaseActivity {
    private ViewPager viewPager;
    private PageIndicatorView pageIndicatorView;
    private Button btn;

    private List<View> pageList = new ArrayList<>();
    private HomeGuideAdapter adapter;

    private Animation showAnim = null;
    private Animation hideAnim = null;
    private boolean isBtnShow = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.guide_activity);

        showAnim = AnimationUtils.loadAnimation(this, R.anim.view_alpha_show);
        hideAnim = AnimationUtils.loadAnimation(this, R.anim.view_alpha_hide);

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        pageIndicatorView = (PageIndicatorView) findViewById(R.id.pageIndicatorView);
        btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent().setClass(GuideGifActivity.this, HomeActivity.class));
                finish();
            }
        });

        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initData() {
        adapter = new HomeGuideAdapter();
        pageList.addAll(createPageList());

        adapter.setData(pageList);
        viewPager.setAdapter(adapter);

        pageIndicatorView.setViewPager(viewPager);

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                if (position == simpleDraweeViewList.size() - 1) {
                    isBtnShow = true;
                } else {
                    isBtnShow = false;
                }
                //System.out.println("position---" + position);
                controllerList.get(position).getAnimatable().start();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                //state 1--开始滑动状态  2--滑动状态 0--结束状态
                if (state == 0) {
                    //System.out.println("state---" + state);
                    if (isBtnShow) {
                        btn.startAnimation(showAnim);
                        btn.setVisibility(View.VISIBLE);
                    } else {
//                        btn.startAnimation(hideAnim);
                        btn.setVisibility(View.GONE);
                    }
                } else if (state == 1) {
                    //System.out.println("state---" + state);
                    if (isBtnShow) {
                        btn.startAnimation(hideAnim);
                        btn.setVisibility(View.GONE);
                    } else {
                        btn.setVisibility(View.GONE);
                    }
                } else {
                    //System.out.println("state---" + state);
                    btn.setVisibility(View.GONE);
                }
            }
        });
    }

    private List<SimpleDraweeView> simpleDraweeViewList = new ArrayList<>();

    private List<SimpleDraweeView> createPageList() {
        simpleDraweeViewList.add(createPageView("asset://com.anysoft/p1.gif"));
        simpleDraweeViewList.add(createPageView("asset://com.anysoft/loading.gif"));
        simpleDraweeViewList.add(createPageView("asset://com.anysoft/loading.gif"));
        simpleDraweeViewList.add(createPageView("asset://com.anysoft/loading.gif"));

        return simpleDraweeViewList;
    }

    private List<DraweeController> controllerList = new ArrayList<>();

    @NonNull
    private SimpleDraweeView createPageView(String gifPath) {
        SimpleDraweeView draweeView = new SimpleDraweeView(this);

        if ("asset://com.anysoft/p1.gif".equals(gifPath)) {
            DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                    .setAutoPlayAnimations(true)//自动播放动画
                    .setUri(Uri.parse(gifPath))//路径
                    .build();
            draweeView.setController(draweeController);
            controllerList.add(draweeController);
        } else {
            /**
             * 下面是主要代码：
             */
            DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                    .setAutoPlayAnimations(false)//自动播放动画
                    .setUri(Uri.parse(gifPath))//路径
                    .build();
            draweeView.setController(draweeController);
            controllerList.add(draweeController);
        }
        return draweeView;
    }
}
